Component({
  behaviors: ['wx://form-field-group'],

  data: {
    address: '点击选择，请勾选哦~',
    isSubmit: false,
    isSucc: false
  },

  lifetimes: {
    ready() {
      this.staticData = {}
    },
  },

  methods: {
    handleAddressTap() {
      wx.chooseLocation({
        success: (res) => {
          let { address, latitude, longitude } = res
          this.staticData.latitude = latitude
          this.staticData.longitude = longitude
          this.setData({
            address
          })
        }
      })
    },
  
    _toast(title) {
      wx.showToast({
        title,
        icon: 'loading',
        duration: 2000
      })    
    },
  
    handleSubmitTap(e) {
      let { message, contact } = e.detail.value
      if(this.data.address === '点击选择，请勾选哦~') {
        this._toast('请选择地址')
        return
      }
      if(message === '') {
        this._toast('请填写说明')
        return
      }
      if(contact === '') {
        this._toast('请填写联系方式')
        return
      }
  
      let data = {
        ...this.staticData,
        ...this.data,
        ...e.detail.value
      }

      wx.request({
        url: 'http://localhost:9000/data', //仅为示例，并非真实的接口地址
        method: 'post',
        data,
        header: {
          'content-type': 'application/x-www-form-urlencoded' // 默认值
        },
        success: (res) => {
          console.log(res)
          this.setData({
            isSucc: true
          })
        },
        complete: () => {
          this.setData({
            isSubmit: true
          })
        }
      })
    }
  }
})